<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
class Notice extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'notice';
    public function manager()
    {
        return $this->belongsTo(Manager::class, 'manager_id');
    }
    /**
     * 发送
     * @return true
     */
    public function send()
    {
        $userIds = User::query()->pluck('id');
        $data = [];
        foreach ($userIds as $id) {
            $data[] = ['user_id' => $id, 'notice_id' => $this->id, 'create_time' => date('Y-m-d H:i:s'), 'status' => 1];
        }
        NoticeUser::query()->insert($data);
        return true;
    }
    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        return ['id' => $this->id, 'name' => $this->name ?? '', 'code' => $this->code ?? '', 'address' => $this->address ?? '', 'mobile' => $this->mobile ?? '', 'img_id' => $this->img_id ?? '', 'img' => stripslashes(($this->img->url ?? ''))];
    }
}